package cn.com.shopec.erp.multiChannel.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.excel.ExcelBaseExport;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.multiChannel.entity.ChannelOrder;
import cn.com.shopec.erp.multiChannel.entity.ChannelOrderItem;
import cn.com.shopec.erp.multiChannel.entity.ChannelOrderReceiver;
import cn.com.shopec.erp.multiChannel.entity.ProductPackage;
import cn.com.shopec.erp.multiChannel.service.IChannelOrderService;
import cn.com.shopec.erp.multiChannel.vo.ChannelOrderVo;
import cn.com.shopec.erp.purchase.entity.StockPurchase;
import cn.com.shopec.erp.purchase.service.IPurchaseRequisitionService;
import cn.com.shopec.erp.purchase.vo.PurchaseRequisitionProductVo;
import cn.com.shopec.erp.stock.excel.impl.ProductPackageExcelExport;
import cn.com.shopec.erp.system.common.UserUtils;
import cn.com.shopec.erp.system.vo.UserVo;
import net.sf.json.JSONArray;

@Controller
@RequestMapping("/order/channelOrder")
public class ChannelOrderController {

	@Resource
	private IPurchaseRequisitionService ipurchaseRequest;
	
	@Resource
	private IChannelOrderService channelOrderService;

	private static final Log log = LogFactory.getLog(ChannelOrderController.class);

	@RequestMapping("toOrder")
	public String toOrder() {
		return "multiChannel/order";
	}

	@RequestMapping("toItem")
	public String toItem(String channelOrderId, String flag, String prodPackageNo, ModelMap map) {
		if (channelOrderId != null && !"".equals(channelOrderId)) {
			ChannelOrderReceiver channelOrderReceiver = channelOrderService.getChannelOrderReceiver(channelOrderId);
			if (channelOrderReceiver != null) {
				map.put("channelOrderReceiver", channelOrderReceiver);
			} else {
				channelOrderReceiver = new ChannelOrderReceiver();
				channelOrderReceiver.setChannelOrderId(channelOrderId);
				map.put("channelOrderReceiver", channelOrderReceiver);
			}
			if (flag != null && !"".equals(flag)) {
				map.put("flag", flag);
				if (prodPackageNo == null || "".equals(prodPackageNo)) {
					ProductPackage productPackage = channelOrderService.getProductPackage(channelOrderId);
					if (productPackage != null) {
						prodPackageNo = productPackage.getProdPackageNo();
					}
				}
				map.put("prodPackageNo", prodPackageNo);
			}
		}
		return "multiChannel/item";
	}

	@RequestMapping("toReturn")
	public String toReturn() {
		return "multiChannel/return";
	}

	@RequestMapping("toProductPackage")
	public String toProductPackage() {
		return "multiChannel/package";
	}

	@RequestMapping("channelOrderPageList")
	@ResponseBody
	public String channelOrderPageList(ChannelOrderVo channelOrderVo, PageQuery pageQuery) throws ParseException {
		PageFinder<ChannelOrder> page = channelOrderService.channelOrderPageList(channelOrderVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}

	@RequestMapping("channelOrderItemPageList")
	@ResponseBody
	public String channelOrderItemPageList(ChannelOrderVo channelOrderVo, PageQuery pageQuery) {
		PageFinder<ChannelOrderItem> page = channelOrderService.channelOrderItemPageList(channelOrderVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}

	@RequestMapping("productPackagePageList")
	@ResponseBody
	public String productPackagePageList(ChannelOrderVo channelOrderVo, PageQuery pageQuery) {
		PageFinder<Map<String, Object>> page = channelOrderService.productPackagePageList(channelOrderVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}
	
	@RequestMapping("exportProductPackage")
	public void exportProductPackage(ChannelOrderVo channelOrderVo, HttpServletResponse response) {
		PageFinder<Map<String, Object>> page = channelOrderService.productPackagePageList(channelOrderVo, null);
		ExcelBaseExport excelBaseExport = new ProductPackageExcelExport(page.getData(), "商品包裹单_"+System.currentTimeMillis(), response);
		try {
			excelBaseExport.wirteExcel();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("createProductPackage")
	@ResponseBody
	public String createProductPackage(ChannelOrderVo channelOrderVo)  {
		UserVo userVo = UserUtils.getUserVo();
		channelOrderVo.setOperatorId(userVo.getId());
		return channelOrderService.createProductPackage(channelOrderVo);
	}

	@RequestMapping("productPackageItemPageList")
	@ResponseBody
	public String productPackageItemPageList(ChannelOrderVo channelOrderVo, PageQuery pageQuery) {
		PageFinder<ChannelOrderItem> page = channelOrderService.productPackageItemPageList(channelOrderVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}

	@RequestMapping("returnOrderItemPageList")
	@ResponseBody
	public String returnOrderItemPageList(ChannelOrderVo channelOrderVo, PageQuery pageQuery) {
		String channelOrderId = channelOrderVo.getChannelOrderId();
		PageFinder page = new PageFinder(pageQuery.getPage(), pageQuery.getPageSize(), 0, new ArrayList());
		if (channelOrderId != null && !"".equals(channelOrderId)) {
			ProductPackage productPackage = channelOrderService.getProductPackage(channelOrderId);
			if (productPackage != null && productPackage.getProdPackageNo() != null) {
				channelOrderVo.setProdPackageNo(productPackage.getProdPackageNo());
				page = channelOrderService.productPackageItemPageList(channelOrderVo, pageQuery);
			} else {
				page = channelOrderService.returnOrderItemPageList(channelOrderVo, pageQuery);
			}
		}
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}
	
	
	/***
	 * 多渠道对外接口同步订单信息
	 */
	@RequestMapping("channelOrdeSync")
	@ResponseBody
	public String channelOrdeSync(ModelMap map,HttpServletRequest request, HttpServletResponse response) {
		try {
			//获取数据json
			String jsonData=request.getParameter("channelOrders");
			JSONArray jsonArray=JSONArray.fromObject(jsonData);
			if(channelOrderService.saveChannelOrder(jsonArray)){
				map.put("status", SZSCMConstant.SUCCESS_INT);
				map.put("message", "SUCCESS");
			}else{
				map.put("status", SZSCMConstant.FAIL_INT);
				map.put("message", "FAIL");
			}
		} catch (Exception e) {
			map.put("status", SZSCMConstant.FAIL_INT);
			map.put("message", "同步订单信息出错,详情为:"+e.getMessage());
			e.printStackTrace();
		}
		return JsonHelper2.toJson(map);
	}
	
	/**
	 * 
	 * @Title: toChannelOrder
	 * @Description: 跳转到添加多渠道订单页面
	 * @return
	 * @throws @author:LiangShunWang
	 * @date: 2016-11-21下午19:21:57
	 */
	@RequestMapping("to_Channel_Order")
	public ModelAndView toChannelOrder(String productlist, ChannelOrderReceiver prv,ChannelOrder order, PageQuery pageQuery, ModelMap model)
			throws Exception {
		model.put("prv", prv);
		model.put("order", order);
		return new ModelAndView("multiChannel/ChannelOrder_add",model);
	}
	
	/**
	 * @methodName：getCommodityList
	 * @Description: 进入选择商品页面
	 * @param ProductVopageQuery
	 * @return
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-22 下午15:10:03
	 */
	@RequestMapping("getCommodityList")
	public ModelAndView getCommodityList(ChannelOrderReceiver productVo, PageQuery pageQuery, ModelMap model)
			throws Exception {
		model.put("productVo", productVo);
		return new ModelAndView("purchase/productReturn_elect", model);
	}

	/**
	 * @methodName：getAjaxCommodityList
	 * @Description: 获取商品
	 * @param ProductVo
	 *            pageQuery
	 * @return
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-22 下午15:10:03
	 */
	@RequestMapping("getAjaxCommodityList")
	@ResponseBody
	public String getAjaxCommodityList(PurchaseRequisitionProductVo purchaseRequisitionProductVo, PageQuery pageQuery)
			throws Exception {
		PageFinder<PurchaseRequisitionProductVo> productVoList = this.ipurchaseRequest.purchaseReturnListToWarehouse(purchaseRequisitionProductVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", productVoList.getData());
		map.put("Total", productVoList.getRowCount());
		return JsonHelper2.toJson(map);
	}

	/**
	 * 
	 * @Title: addBrand
	 * @Description: 添加多渠道订单数据
	 * @param brandVo
	 * @param map
	 * @return
	 * @throws Exception
	 * @throws @author: LiangShunWang
	 * @date: 2016-11-21下午19:16:58
	 */
	@RequestMapping("/add_purchaseReturn")
	@ResponseBody
	public String addPurchaseReturn(ChannelOrderReceiver channel, String defectives, ModelMap map,ChannelOrder order) {
		try {
			JSONArray jsonArray = JSONArray.fromObject(defectives);
			@SuppressWarnings("unchecked")
			List<ChannelOrderItem> stockInProducts = (List<ChannelOrderItem>) JSONArray.toList(jsonArray, ChannelOrderItem.class);
			
			this.channelOrderService.addPurchaseReturn(stockInProducts, channel,order);
//			this.poReturnService.addPurchaseReturn(stockInProducts,purchase);
		} catch (Exception e) {
			log.error("addBrand:" + e.getMessage(), e);
			return SZSCMConstant.FAIL;
		}
		return SZSCMConstant.SUCCESS;
	}

	
}
